package com.jack.lib

import android.util.Log
import android.webkit.JsResult
import android.webkit.WebChromeClient
import android.webkit.WebView
import androidx.lifecycle.coroutineScope
import com.blankj.utilcode.util.ResourceUtils
import com.jack.lib.base.BaseFragment
import com.jack.lib.core.ext.toastS
import com.jack.lib.core.utils.PackageInfoUtils
import com.jack.lib.databinding.FragmentMainBinding
import com.koushikdutta.async.http.AsyncHttpClient
import kotlinx.coroutines.*
import org.java_websocket.server.WebSocketServer
import java.net.InetSocketAddress
import java.net.URI


/**
 * author : chong.huang
 * create : 2022/1/3
 * desc   :
 */
class MainFragment : BaseFragment<FragmentMainBinding>() {

    override fun initLayout() {

        lifecycle.coroutineScope.launch {
            val ipAddress = PackageInfoUtils.getIPAddress(requireContext())
        /*    val httpServer = AsyncHttpServer()
            httpServer.listen(AsyncServer.getDefault(), 5000)

            httpServer.websocket("/live") { webSocket, request ->
                Log.e("test", "添加webSocket")
                toastS("添加webSocket")

                //Use this to clean up any references to your websocket
                webSocket.closedCallback = CompletedCallback { ex ->
                    try {
                        if (ex != null) Log.e("test", "An error occurred", ex)
                    } finally {

                    }
                }
                webSocket.setStringCallback { s -> if ("Hello Server" == s) webSocket.send("Welcome Client!") }
            }
*/

            Log.e("test", "file:android_asset/socket.html?socketUrl=ws://" + ipAddress + ":5000")





            async {
                withContext(Dispatchers.IO) {
                    val server: WebSocketServer = SimpleServer(InetSocketAddress(4999))
                    server.isReuseAddr = true
                    server.run()
                }

            }
            delay(2000)
            withContext(Dispatchers.Main) {
                binding.webView.settings.javaScriptEnabled = true

                val socketUrl = "socketUrl=ws://$ipAddress:4999"
                val pageTime = "pageTime=5"
                val refreshTime = "refreshTime=${System.currentTimeMillis()}"
                val sn = "sn=121212121"
                binding.webView.loadUrl("file:///android_asset/aaa/index.html?$socketUrl&$pageTime&$refreshTime&$sn")
//                ExampleClient(URI("ws://192.168.31.75:5000")).connect()
            }





//

/*
            AsyncHttpClient.getDefaultInstance().websocket("ws://" + "192.168.31.75" + ":5000/live", "",
                AsyncHttpClient.WebSocketConnectCallback { ex, webSocket ->
                    if (ex != null) {
                        Log.e("test", "异常")
                        Log.e("test", Log.getStackTraceString(ex))
                        ex.printStackTrace()
                        return@WebSocketConnectCallback
                    }
                    toastS("添加webSocket")
                    Log.e("test", "连接成功")
                })
*/
        }

    }
}